package com.xjtu.platform.user.assister.consultArrange.dao;


import com.xjtu.platform.user.assister.consultArrange.FillBlankDto;
import com.xjtu.platform.user.assister.consultArrange.FreeTimeDto;
import com.xjtu.platform.user.assister.consultArrange.FreeTimeModel;
import com.xjtu.platform.common.model.Consult;
import com.xjtu.platform.common.model.Duty;
import com.xjtu.platform.common.model.FirstVisit;
import com.xjtu.platform.common.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ConsultArrangeDao {
    @Select("select * from duty,consult where duty.duty_id = consult.duty_id and " +
            "visit_id=#{visit_id} order by count_num")
    List<FillBlankDto> getAllConsultOfVisit(FirstVisit dto);

    @Select("select user.user_id,user.user_name,user.email from user,user_role where user.user_id = user_role.user_id and " +
            "user_role.role_id = \"consultor\"")
    List<User> getAllTeachers();

    @Select("select time_id from duty where place_name=#{place_name} and day=#{day} and user_id = #{consultant_id}")
    List<FreeTimeModel> getFreeTime(FreeTimeDto freeTimeDto);

    @Select("select time_id,start_time,end_time from time where time_id=#{time_id}")
    FreeTimeModel getTimeInfo(String time);

    @Select("select * from consult,duty where consult.duty_id = duty.duty_id and visit_id=#{visit_id} and " +
            "isNull(day) and consult.count_num >= #{count_num} order by count_num")
    List<FillBlankDto> getConsultNeedToBlank(FillBlankDto dto);

    @Select("select count(*) from duty where day=#{day} and time_id = #{time_id} " +
            "and (user_id = #{consultant_id}) or place_name = #{place_name}")
    Integer checkDuty(FillBlankDto fillBlankDto);

    @Insert("insert into duty(duty_id, time_id, day, place_name, user_id) " +
            "values(#{duty_id}, #{time_id}, #{day}, #{place_name}, #{consultant_id})")
    boolean insertDuty(FillBlankDto fillBlankDto);

    @Update("update consult set state = \"3\" where consult_id = #{consult_id}")
    void updateConsultDuty(FillBlankDto fillBlankDto);

    @Select("select count(*) from consult where visit_id = #{visit_id}")
    Integer checkHasConsultToArrange(FirstVisit dto);

    @Insert("insert into consult(visit_id, consult_id, count_num, duty_id,state) " +
            "values(#{visit_id},#{consult_id},#{count_num},#{duty_id},1)")
    void insertInitConsult(Consult consult);

    @Insert("insert into duty(duty_id) values(#{duty_id})")
    void insertInitDuty(Duty duty);

    @Update("update duty set time_id = #{time_id}, day=#{day}, place_name=#{place_name}, " +
            "user_id=#{user_id} where duty_id=#{duty_id}")
    void updateDuty(FillBlankDto currentDto);

    @Select("select count(*) from time,consult,duty where consult.visit_id = #{visit_id} and " +
            "consult.duty_id=duty.duty_id and duty.time_id = time.time_id")
    Integer checkNumOfFinishArrange(FirstVisit dto);

    @Update("update consult set state = #{state} where visit_id=#{visit_id} and " +
            "count_num = #{count_num}")
    void updateConsult(Consult consult);

    @Update("update first_visit set status = #{status} where visit_id=#{visit_id}")
    void updateFirstVisit(FirstVisit dto);
}
